package aip.ui.attachment.srvc;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.StringUtils;

import aip.core.dao.attachment.AttachmentDao;
import aip.core.model.attachment.Attachment;
import aip.core.model.attachment.Photo;
import aip.ui.attachment.web.spring.UploadForm;

import com.googlecode.horncomb.bean.paged.PagedList;

/**
 * Attachment Service
 * <p>
 * 负责人/principal: Daniel<br/>
 * 修改记录/revision:<br/>
 * 日期:<br/>
 * 修改人:<br/>
 * 修改说明:
 * @author RUN
 * @version $Id: codetemplates.xml 2007 2009-05-08 08:36:14Z shiw $
 */
public class AttachmentService
{
    private static final Integer PAGE_SIZE = 8;
    private AttachmentDao attachmentDao;

    public void get(UploadForm form) throws Exception
    {
    }

    public Map<String, Object> showReference(UploadForm form) throws Exception
    {
        return null;
    }

    public void save(UploadForm form) throws Exception
    {
        for (Attachment attachment : form.getAttachments())
        {
            this.attachmentDao.save(attachment);
        }
    }

    public void save(Attachment attachment) throws Exception
    {
        this.attachmentDao.save(attachment);
    }

    /**
     * 更新照片附件
     * @param attachment
     * @throws Exception
     */
    public void updatePhoto(Photo photo) throws Exception
    {
        this.attachmentDao.updatePhoto(photo);
    }

    /**
     * 删除照片附件的content、thumbnail
     * @param form
     * @throws Exception
     */
    public void deletePhotoContent(UploadForm form) throws Exception
    {
        this.attachmentDao.deletePhotoContent(form.getId(), form.getAtchType());
    }

    /**
     * 获取附件列表，取得除文件内容外的其他信息
     * @param form
     * @return
     * @throws Exception
     */
    public Map<String, Object> listReference(UploadForm form) throws Exception
    {
        Map<String, Object> ref = new HashMap<String, Object>();
        if (!StringUtils.hasText(form.getAtchType()))
        {
            form.setAtchType(Attachment.TYPE_COMMON);
        }
        List<? extends Attachment> attachments;
        // 照片附件，分页显示
        if (form.getAtchType().indexOf(Attachment.TYPE_PHOTO) != -1)
        {
            PagedList<List<? extends Attachment>> pl = this.attachmentDao
                    .queryByEntity(form.getEntityRefId(), form.getAtchType(),
                            form.getPage(), PAGE_SIZE);
            attachments = pl.getSource();
            ref.put("totalRows", pl.getNrOfElements());
            ref.put("pageCount", pl.getPageCount());
            ref.put("photoCnts", attachments.size());
            // 获取主照片
            if (pl.getPage() == 1 && !attachments.isEmpty())
            {
                ref.put("primaryPhoto", attachments.get(0));
            }
            else if (pl.getPage() > 1)
            {
                ref.put("primaryPhoto", this.attachmentDao.getPrimaryPhoto(form
                        .getEntityRefId(), form.getAtchType()));
            }
        }
        // 通用附件、链接附件，取出除文件内容外的其他信息
        else
        {
            attachments = this.attachmentDao.queryByEntity(form
                    .getEntityRefId(), form.getAtchType());
        }
        form.setAttachments(attachments);
        return ref;
    }

    /**
     * 获取附件内容
     * @param id
     * @param type
     * @return
     */
    public Attachment getWithContent(String id, String type) throws Exception
    {
        if (!StringUtils.hasText(type))
        {
            type = Attachment.TYPE_COMMON;
        }
        return this.attachmentDao.getWithContent(id, type);
    }

    /**
     * 获取图片附件的缩略图
     * @param id
     * @param type
     * @return
     */
    public Photo getWithThumb(String id, String type) throws Exception
    {
        if (!StringUtils.hasText(type))
        {
            type = Attachment.TYPE_PHOTO;
        }
        return this.attachmentDao.getWithThumb(id, type);
    }

    /**
     * 删除附件（仅删除附件与文件关联，真实附件使用quartz定期删除）
     * @param form
     * @throws Exception
     */
    public void delete(UploadForm form) throws Exception
    {
        this.attachmentDao.deleteRef(form.getId(), form.getEntityRefId(), form
                .getAtchType());
    }

    /**
     * @param attachmentDao
     *        the attachmentDao to set
     */
    public void setAttachmentDao(AttachmentDao attachmentDao)
    {
        this.attachmentDao = attachmentDao;
    }
}
